<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>10_计算属性案例</title>
</head>

<!-- 需求: 计算出总共有多少本书 -->

<body>
    <div id="app">
        <h4>书本数量:{{booksNumber}}</h4>
    </div>
</body>
<script src="../vue.js"></script>
<script>
    const vm = new Vue({
        el: '#app',
        data: {
            books: [{
                name: '水浒传',
                number: 101
            }, {
                name: '红楼梦',
                number: 79
            }, {
                name: '西游记',
                number: 89
            }, {
                name: '三国',
                number: 65
            }]
        },
        computed: {
            booksNumber() {
                let result = 0
                    // for (let i = 0; i < this.books.length; i++) {
                    //     result += this.books[i].number
                    // }
                    // return result
                for (let i in this.books) {
                    result += this.books[i].number
                }
                return result
            }
        }
    })
</script>

</html>